<?php

use \app\service\auth;
use Illuminate\Database\Capsule\Manager as DB;


class Agent_GoodsController extends CommonController
{
    public function categoryAction()
    {
        $data = categoryModel::where('pid', 1)
            ->where('status', 'normal')
            ->select(['id', 'name', DB::raw('0 as number')])
            ->get();
        json(['code' => 1, 'msg' => '获取成功', 'data' => $data]);
    }

    public function goodsAction()
    {
        $page = $this->get('page', 1);
        $per_page = $this->get('per_page', 10);
        $offset = ($page - 1) * $per_page;
        if ($offset < 0) {
            $offset = 0;
        }

        $keywords = $this->get('keywords');
        $category_ids = categoryModel::where('pid', 1)
            ->where('status', 'normal')
            ->pluck('id')
            ->toArray();
        $total = goodsModel::where('status', 'up')->where('title', 'like', "%{$keywords}%")->count();
        $goods = goodsModel::where('status', 'up')
            ->where('title', 'like', "%{$keywords}%")
            ->offset($offset)
            ->limit($per_page)
            ->get()
            ->toArray();

        $data = [];
        $good_sku_price_cnt = 0;

        $sku_price = goodsskupriceModel::whereIn('goods_id', array_column($goods, 'id'))
            ->where('status', 'up')
            ->select(['id', 'goods_id', 'price', 'goods_sku_text'])
            ->get();
        foreach ($goods as $goods_item) {
            $item_category_ids = explode(',', $goods_item['category_ids']);
            $item_category_ok = false;
            foreach ($item_category_ids as $item_category_id) {
                if (in_array($item_category_id, $category_ids)) {
                    $item_category_ok = true;
                    break;
                }
            }
            if ($item_category_ok) {
                $goods_sku_price = [];
                foreach ($sku_price as $sku_price_item) {
                    if ($sku_price_item->goods_id == $goods_item['id']) {
                        $original_goods_sku_text = $sku_price_item->getRawOriginal('goods_sku_text');
                        $sku_price_item = $sku_price_item->toArray();
                        $sku_price_item['goods_sku_text'] = $original_goods_sku_text;
                        $goods_sku_price[] = $sku_price_item;
                        $good_sku_price_cnt++;
                        if ($good_sku_price_cnt > 3000) {
                            break 2;
                        }
                    }
                }
                $goods_item['sku_price'] = $goods_sku_price;
                $data[] = $goods_item;
            }
        }

        if (!empty($data)) {
            $data[0]['good_sku_price_cnt'] = $good_sku_price_cnt;
        }

        $result = [
            'current_page' => $page,
            'last_page' => ceil($total / $per_page),
            'per_page' => $per_page,
            'total' => $total,
            'data' => $data,
        ];
        json(['code' => 1, 'msg' => '获取成功', 'data' => $result]);
    }
}
